package com.ctg.itrdc.k8sconsole.control;

import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.imageconsole.common.bean.CommonResult;
import com.ctg.itrdc.imageconsole.common.bean.ReturnCodeDefine;
import com.ctg.itrdc.imageconsole.data.dto.K8sRcDTO;
import com.ctg.itrdc.k8sconsole.service.IDeveloperService;
import com.ctg.itrdc.paspsdk.common.utils.json.JSONUtils;

/**
 * 
 * @description:提供给开发者门户 调用的接口
 * @version: v1.0.0 
 * @author: Piao.Wu
 * 
 * Modification History:
 * Date               Author      Version     Description
 * -----------------------------------------------------------------
 * 2016年8月23日 上午10:56:58     Piao.Wu       v1.0.0        create
 *
 */
@Controller
@RequestMapping("/k8s")
public class DeveloperController {
	
	@Resource
	private IDeveloperService developerService;
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	
	/**
	 * 
	 * @Description: 根据集群编码 查询集群下的资源信息
	 * @author Piao.Wu 2016年9月23日 下午3:05:52
	 * @param clusterCode
	 * @return
	 * CommonResult
	 *
	 */
	@RequestMapping(value = "/queryK8sResources", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryK8sResources(@RequestParam("clusterCode") String clusterCode) {
		
		Map<String, Object> map = null;
		String jsonStr = null;
		for(int i=0;i<3;i++){
			try {
				jsonStr = developerService.queryK8sResources(clusterCode);
				map = JSONUtils.jsonToMap(jsonStr);
				break;
			} catch (Exception e) {
				logger.error("查询出错！",e);
//				jsonStr = e.getLocalizedMessage();
			}
		}
		if(map==null||map.size()==0){
			return new CommonResult(ReturnCodeDefine.Code.C1101, "请求出错了！");
		}
		return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, map);
	}
	
	
	/**
	 * 
	 * @Description: 查询指定集群下面多个命名空间下面的多个rc
	 * @author Piao.Wu 2016年9月23日 下午4:49:07
	 * @param k8sRcDTO
	 * @return
	 * CommonResult
	 *
	 */
	@RequestMapping(value = "/queryRcs", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult queryRcs(@RequestBody K8sRcDTO k8sRcDTO) {
		Map<String, Object> map = null;
		String jsonStr = null;
		for(int i=0;i<3;i++){
			try {
				jsonStr = developerService.queryRcs(k8sRcDTO);
				map = JSONUtils.jsonToMap(jsonStr);
				break;
			} catch (Exception e) {
				logger.error("查询出错！",e);
				jsonStr = e.getLocalizedMessage();
			}
		}
		if(map==null||map.size()==0){
			return new CommonResult(ReturnCodeDefine.Code.C1101, jsonStr);
		}
		return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, map);
	}

	
	
	
	
	
	
}
